import requests
import json
import time
import csv
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36',
}
# 不选择保留成csv文件
# csvfile = open('知乎评论.csv', 'w', newline='', encoding='utf-8')
# writer = csv.writer(csvfile)
# writer.writerow(['id', 'created_time', 'author', 'content'])

""" 获取用户的回答
:is_end: 是否获取到最后一页(默认可以不获取 为了方便只保留了前50页)
:answer_id: 
:created_time: 创建时间
:author name:作者姓名
:content: 利用xpath解析加入其中的内容
:return: l = list()
"""
def GetAnswers(question_id):
    offset = 0
    while True:
        url = 'https://www.zhihu.com/api/v4/questions/{0}/answers' \
              '?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%' \
              '2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%' \
              '2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%' \
              '2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%' \
              '2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%' \
              '2Cis_nothelp%2Cis_recognized%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%' \
              '2Cbadge%5B%2A%5D.topics%3Bdata%5B%2A%5D.settings.table_of_content.enabled&limit=5&offset={1}&platform=desktop&' \
              'sort_by=default'.format(question_id,offset)

        state=1
        while state:
            try:
                res = requests.get(url, headers=headers, timeout=(3, 7))
                state=0
            except Exception as e:
                print(e)
                continue

        res.encoding = 'utf-8'
        jsonAnswer = json.loads(res.text)
        # print(jsonAnswer)
        is_end = jsonAnswer['paging']['is_end']

        for data in jsonAnswer['data']:
            l = list()
            answer_id = str(data['id'])
            answer_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data['created_time']))
            author_name = data['author']['name']
            answer_content = ''.join(etree.HTML(data['content']).xpath('//p//text()'))
            l.append(answer_id)
            l.append(answer_time)
            l.append(author_name)
            l.append(answer_content)
            print(answer_content)
            print(len(answer_content))

            # if data['admin_closed_comment'] == False and data['can_comment']['status'] and data['comment_count'] > 0:
            #     GetComments(answer_id)

        offset += 5
        print('获取到到第{0}页'.format(int(offset / 5)))

        # if is_end:
        #     break
        if offset >= 50:
            break

        time.sleep(1)

"""
获取用户的评论(暂时不用)
"""
def GetComments(answer_id):
    j = 0
    while True:
        url = 'https://www.zhihu.com/api/v4/answers/{0}/root_comments?order=normal&limit=20&offset={1}&status=open'.format(
            answer_id, j)

        state=1
        while state:
            try:
                res = requests.get(url, headers=headers, timeout=(3, 7))
                state=0
            except:
                continue

        res.encoding = 'utf-8'
        jsonComment = json.loads(res.text)
        is_end = jsonComment['paging']['is_end']

        for data in jsonComment['data']:
            l = list()
            comment_id = str(answer_id) + "_" + str(data['id'])
            l.append(comment_id)
            l.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data['created_time'])))
            l.append(data['author']['member']['name'])
            l.append(''.join(etree.HTML(data['content']).xpath('//p//text()')))
            # writer.writerow(l)
            print(l)

            for child_comments in data['child_comments']:
                l.clear()
                l.append(str(comment_id) + "_" + str(child_comments['id']))
                l.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(child_comments['created_time'])))
                l.append(child_comments['author']['member']['name'])
                l.append(''.join(etree.HTML(child_comments['content']).xpath('//p//text()')))
                answer_content = ''.join(etree.HTML(child_comments['content']).xpath('//p//text()'))
                print(answer_content)
                print(len(str(answer_content)))
        j += 20
        if is_end:
            break

        time.sleep(1)


GetAnswers('344022718')

